package gui.addArcherWindow;

import gui.InformationDisplay;
import gui.helper.CorrectnessTester;

import java.awt.Color;
import java.sql.SQLException;
import java.util.Observable;
import java.util.Observer;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;

import tournamentData.TournamentData;
import core.Constants;
import core.Date;
import core.Person;
import dataBase.DbHolder;
import db.newversion.ArchersDB;

public class AddArcherWindowController implements Observer {

	public AddArcherWindowController() {
		DbHolder.getInstance().getArcherData().addObserver(this);
	}

	private ComboBoxModel comboBoxModelFactory(Object [] content) {
		return new DefaultComboBoxModel(content);
	}

	/**
	 * adds all required items to the specific dropdown menus
	 */
	public void setDropDownMenus() {

		String [] nations = DbHolder.getInstance().getArcherData().getAllNations();
		java.util.Arrays.sort(nations);
		AddArcherWindow.getInstance().getNationDropDown().setModel(this.comboBoxModelFactory(nations));
		((DefaultComboBoxModel) AddArcherWindow.getInstance().getNationDropDown().getModel()).addElement("");
		((DefaultComboBoxModel) AddArcherWindow.getInstance().getNationDropDown().getModel()).setSelectedItem("");

		String [] countries = DbHolder.getInstance().getArcherData().getAllCountries();
		java.util.Arrays.sort(countries);
		AddArcherWindow.getInstance().getCountryDropDown().setModel(this.comboBoxModelFactory(countries));
		((DefaultComboBoxModel) AddArcherWindow.getInstance().getCountryDropDown().getModel()).addElement("");
		((DefaultComboBoxModel) AddArcherWindow.getInstance().getCountryDropDown().getModel()).setSelectedItem("");

		String [] bowCathegories = DbHolder.getInstance().getArcherData().getAllBowCathegories();
		java.util.Arrays.sort(bowCathegories);
		AddArcherWindow.getInstance().getBowCathegoryDropDown().setModel(this.comboBoxModelFactory(bowCathegories));
		((DefaultComboBoxModel) AddArcherWindow.getInstance().getBowCathegoryDropDown().getModel()).addElement("");
		((DefaultComboBoxModel) AddArcherWindow.getInstance().getBowCathegoryDropDown().getModel()).setSelectedItem("");

		String [] ageClasses = DbHolder.getInstance().getArcherData().getAllAgeClasses();
		java.util.Arrays.sort(ageClasses);
		AddArcherWindow.getInstance().getAgeClassDropDown().setModel(this.comboBoxModelFactory(ageClasses));
		((DefaultComboBoxModel) AddArcherWindow.getInstance().getAgeClassDropDown().getModel()).addElement("");
		((DefaultComboBoxModel) AddArcherWindow.getInstance().getAgeClassDropDown().getModel()).setSelectedItem("");

		String [] clubs = DbHolder.getInstance().getArcherData().getAllClubs();
		java.util.Arrays.sort(clubs);
		AddArcherWindow.getInstance().getClubDropDown().setModel(this.comboBoxModelFactory(clubs));
		((DefaultComboBoxModel) AddArcherWindow.getInstance().getClubDropDown().getModel()).addElement("");
		((DefaultComboBoxModel) AddArcherWindow.getInstance().getClubDropDown().getModel()).setSelectedItem("");

		AddArcherWindow.getInstance().getGenderDropDown().setModel(this.comboBoxModelFactory(new String [] { Constants.MALE, Constants.FEMALE }));
	}

	public void updateAgeInformation(Date birthDate) {

		int age = Date.getAge(birthDate);

		if ((age < 100) && !birthDate.equals(new Date("blubb"))) { // blubb
			// generates
			// error
			// date...
			AddArcherWindow.getInstance().getAgeInformationLabel().setText(new Integer(age).toString());
		} else {
			AddArcherWindow.getInstance().getAgeInformationLabel().setText("-");
		}
	}

	public void saveEntry() {
		boolean error = false;

		String name;
		String surName;
		String nation;
		String country;
		String bowCathegory;
		String ageClass;
		String club;
		String gender;
		boolean license;
		boolean countryRating;
		String birthDate;

		error = CorrectnessTester.testCorrectness(error, (name = AddArcherWindow.getInstance().getNameTextBox().getText()).isEmpty(), AddArcherWindow.getInstance().getNameLabel());
		error = CorrectnessTester.testCorrectness(error, (surName = AddArcherWindow.getInstance().getSurNameTextBox().getText()).isEmpty(), AddArcherWindow.getInstance().getSurNameLabel());
		error = CorrectnessTester.testCorrectness(error, (nation = AddArcherWindow.getInstance().getNationDropDown().getSelectedItem().toString()).isEmpty(), AddArcherWindow.getInstance()
				.getNationLabel());
		error = CorrectnessTester.testCorrectness(error, (country = AddArcherWindow.getInstance().getCountryDropDown().getSelectedItem().toString()).isEmpty(), AddArcherWindow.getInstance()
				.getCountryLabel());
		error = CorrectnessTester.testCorrectness(error, (bowCathegory = AddArcherWindow.getInstance().getBowCathegoryDropDown().getSelectedItem().toString()).isEmpty(), AddArcherWindow.getInstance()
				.getBowCathegoryLabel());
		error = CorrectnessTester.testCorrectness(error, (ageClass = AddArcherWindow.getInstance().getAgeClassDropDown().getSelectedItem().toString()).isEmpty(), AddArcherWindow.getInstance()
				.getAgeClassLabel());
		error = CorrectnessTester.testCorrectness(error, (club = AddArcherWindow.getInstance().getClubDropDown().getSelectedItem().toString()).isEmpty(), AddArcherWindow.getInstance().getClubLabel());

		AddArcherWindow.getInstance().getBirthDateTextBox().setText(new Date(AddArcherWindow.getInstance().getBirthDateTextBox().getText()).toString());

		gender = AddArcherWindow.getInstance().getGenderDropDown().getSelectedItem().toString();
		license = AddArcherWindow.getInstance().getLicenseCheckBox().isSelected();
		countryRating = AddArcherWindow.getInstance().getCountryRatingCheckBox().isSelected();
		birthDate = new Date(AddArcherWindow.getInstance().getBirthDateTextBox().getText()).toString();

		if (!error) {

			try {
				int id = ArchersDB.getNextID();
				Person newPerson = new Person(name, surName, nation, country, bowCathegory, ageClass, club, gender, license, countryRating, birthDate);
				DbHolder.getInstance().getArcherData().insertArcher(newPerson);
				InformationDisplay saveInformation = new InformationDisplay(AddArcherWindow.getInstance().getInformationLabel(), "Eintrag gespeichert", Color.green, 2000);
				saveInformation.start(); // display the "saved-information"

				if (AddArcherWindow.getInstance().getIsParticipatingCheckbox().isSelected()) {
					TournamentData.getInstance().addRemovePlayer(id);
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			this.resetAllTextFields();
		}
	}

	private void resetAllTextFields() {
		AddArcherWindow.getInstance().getNameTextBox().setText("");
		AddArcherWindow.getInstance().getSurNameTextBox().setText("");
		AddArcherWindow.getInstance().getBirthDateTextBox().setText("");
		AddArcherWindow.getInstance().getAgeInformationLabel().setText("-");
		AddArcherWindow.getInstance().getIsParticipatingCheckbox().setSelected(false);
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		this.setDropDownMenus();
	}
}
